<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>表单数据保存</title>
    <script>
        window.onload = function() {
            // 恢复表单数据（如果存在）
            const nameInput = document.getElementById('name');
            const emailInput = document.getElementById('email');

            nameInput.value = sessionStorage.getItem('name') || '';
            emailInput.value = sessionStorage.getItem('email') || '';

            // 监听表单输入事件，实时保存数据到 sessionStorage
            nameInput.oninput = function() {
                sessionStorage.setItem('name', this.value);
            };

            emailInput.oninput = function() {
                sessionStorage.setItem('email', this.value);
            };
        };
    </script>
</head>
<body>
    <form>
        <label for="name">姓名:</label>
        <input type="text" id="name" name="name"><br><br>
        <label for="email">邮箱:</label>
        <input type="email" id="email" name="email"><br><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>